package com.cade.agriculturemall.view.register;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.LinkMovementMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cade.agriculturemall.model.RegisterEntity;
import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseActivity;
import com.cade.agriculturemall.util.ToastUtils;
import com.cade.agriculturemall.view.register.registermycode.RegisterMyCodeActivity;
import com.cade.agriculturemall.view.serviceAndPrivacy.ServiceAndPrivacyActivity;
import com.cade.agriculturemall.widget.CleanEditText;

public class RegisterActivity extends BaseActivity<RegisterContract.IPresenter> implements RegisterContract.IView, View.OnClickListener, View.OnFocusChangeListener {
    private Button mBtnRegister;
    private EditText mEtPhone;
    private CleanEditText mEtPswFirst;
    private CleanEditText mEtPswTwice;
    private RadioButton mRadioButton;
    private RelativeLayout mRlRadioButton;
    private RegisterEntity mEntity = new RegisterEntity();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
    }

    @Override
    public void setupView() {
        setupToolbar(true);
        mBtnRegister = (Button) findViewById(R.id.register_btn_register);
        mEtPhone = (EditText) findViewById(R.id.register_et_phone);
        mEtPswFirst = (CleanEditText) findViewById(R.id.register_et_password_first);
        mEtPswTwice = (CleanEditText) findViewById(R.id.register_et_password_twice);
        mRadioButton = (RadioButton) findViewById(R.id.register_rbt);
        mRlRadioButton = (RelativeLayout) findViewById(R.id.register_ll_radiobtn);
        mBtnRegister.setOnClickListener(this);
        mEtPswFirst.setOnCleanFocusChangeListener(this);
        mEtPswTwice.setOnCleanFocusChangeListener(this);
        mRlRadioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRadioButton();
            }
        });
        mRadioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRadioButton();
            }
        });
        imageViewFirt = (ImageView) findViewById(R.id.register_image_password_first);
        imageViewTwice = (ImageView) findViewById(R.id.register_image_password_twice);
        alterPswVision(mEtPswFirst, imageViewFirt);
        alterPswVision(mEtPswTwice, imageViewTwice);
    }

    private int state = 0;
    private ImageView imageViewFirt;
    private ImageView imageViewTwice;

    /**
     * 点击mIVEye设置密码可见/不可见
     */

    public void alterPswVision(final EditText editText, final ImageView imageView) {
        // mPresenter.checkSendVerifyButtonStatus();
        editText.setTransformationMethod(PasswordTransformationMethod.getInstance());//隐藏密码
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (state) {
                    case 0:
                        imageView.setImageResource(R.mipmap.see);
                        editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                        state++;
                        break;
                    case 1:
                        imageView.setImageResource(R.mipmap.no_see);
                        editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                        state--;
                        break;
                }
            }
        });
    }

    public void setRadioButton() {
        Boolean isSelected = mRadioButton.isChecked();
        mRadioButton.setChecked(!isSelected);
    }

    @Override
    public void setupEvent() {
        TextView tvContract = (TextView) findViewById(R.id.register_tv_contract);
        //将TextView的显示文字设置为SpannableString
        tvContract.setText(getClickableSpan());
        //设置该句使文本的超连接起作用
        tvContract.setMovementMethod(LinkMovementMethod.getInstance());
    }

    //设置超链接文字
    private SpannableString getClickableSpan() {
        SpannableString spanStr = new SpannableString("已阅读并同意用户协议");
        //设置下划线文字
        spanStr.setSpan(new UnderlineSpan(), 6, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的前景色
        spanStr.setSpan(new ForegroundColorSpan(Color.RED), 6, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //设置文字的单击事件
        spanStr.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                startActivity(new Intent(getApplication(), ServiceAndPrivacyActivity.class));
            }
        }, 6, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        return spanStr;
    }

    @Override
    public void setupData() {

    }

    @Override
    public RegisterContract.IPresenter setupPresenter() {
        return new RegisterPresenter(this);
    }


    @Override
    public void countDownVerifyCodeBtn(String time) {

    }


    public void selectInfo() {
        mEntity.setPhone(mEtPhone.getText().toString().replaceAll(" ", ""));
        mEntity.setPswFirst(mEtPswFirst.getText().toString());
        mEntity.setPswTwice(mEtPswTwice.getText().toString());
        mEntity.setAgree(mRadioButton.isChecked());

    }

    @Override
    public void onClick(View v) {
        selectInfo();
        mPresenter.register(mEntity);

    }

    public void goRegisterCode() {
        Intent intent = new Intent(this, RegisterMyCodeActivity.class);
        intent.putExtra("mobilePhoneNumber",mEntity.getPhone());
        intent.putExtra("password",mEntity.getPswTwice());
        startActivity(intent);
    }


    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        String mFirstPsw = mEtPswFirst.getText().toString();
        String mTwicePsw = mEtPswTwice.getText().toString();

        if (!TextUtils.isEmpty(mTwicePsw) && !TextUtils.isEmpty(mFirstPsw)) {
            compare(mTwicePsw, mFirstPsw);
        }
    }

    public void compare(String s1, String s2) {
        if (s1.equals(s2)) {
        } else {
            ToastUtils.showToast("两次密码输入不同，请重新输入");
        }
    }
}
